/*******************************************************************************
 * @project: aohuaproject
 * @package: com.aohua.px
 * @file: ECRUpdateEventAction.java
 * @author: fionn
 * @created: 2015年11月24日
 * @purpose:
 * 
 * @version: 1.0
 * 
 * 
 * Copyright 2015 HAND All rights reserved.
 ******************************************************************************/
package com.aohua.px;

import com.agile.api.APIException;
import com.agile.api.IAgileSession;
import com.agile.api.ICell;
import com.agile.api.IChange;
import com.agile.api.IDataObject;
import com.agile.api.INode;
import com.agile.px.ActionResult;
import com.agile.px.EventActionResult;
import com.agile.px.ICreateEventInfo;
import com.agile.px.ICustomAction;
import com.agile.px.IEventAction;
import com.agile.px.IEventInfo;
import com.agile.px.IUpdateTitleBlockEventInfo;
import com.hand.agile.utils.CellUtils;

/**
 * @author fionn
 *
 */
public class ECRUpdateEventAction implements IEventAction, ICustomAction {

    private static final String risk1 = "Ⅰ";
    private static final String risk2 = "Ⅱ";
    private static final String risk3 = "Ⅲ";
    /**
     * 
     */
    public ECRUpdateEventAction() {
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see com.agile.px.IEventAction#doAction(com.agile.api.IAgileSession, com.agile.api.INode, com.agile.px.IEventInfo)
     */
    @Override
    public EventActionResult doAction(IAgileSession session, INode arg1,
            IEventInfo arg2) {
        EventActionResult result = null;
        try {
            IChange change =  null;
            if(arg2 instanceof IUpdateTitleBlockEventInfo) {
                IUpdateTitleBlockEventInfo info = (IUpdateTitleBlockEventInfo)arg2;
                change = (IChange)info.getDataObject();
            }else if(arg2 instanceof ICreateEventInfo) {
                ICreateEventInfo info = (ICreateEventInfo)arg2;
                change = (IChange)info.getDataObject();
            }else{
                
            };
            result = new EventActionResult(arg2, doAction(change));
        } catch (APIException e) {
            e.printStackTrace();
            result= new EventActionResult(arg2, new ActionResult(ActionResult.EXCEPTION, e));
        }
        return result;
    }

    /* (non-Javadoc)
     * @see com.agile.px.ICustomAction#doAction(com.agile.api.IAgileSession, com.agile.api.INode, com.agile.api.IDataObject)
     */
    @Override
    public ActionResult doAction(IAgileSession session, INode arg1,
            IDataObject arg2) {
        IChange change = (IChange)arg2;
        return doAction(change);
    }

    private ActionResult doAction(IChange change) {
        ActionResult result = null;
        try {
            // 严重程度
            ICell cell = change.getCell(Integer.valueOf(1545));
            String yzcd = CellUtils.getCellStringValue(cell);
//            发生频率
            cell = change.getCell(Integer.valueOf(1546));
            String fspl = CellUtils.getCellStringValue(cell);
            if("".equals(yzcd) || "".equals(fspl)) return new ActionResult(ActionResult.STRING, "[严重程度] 或 [发生频率] 未填写，不能计算 [风险等级]。");
            int x = Integer.parseInt(yzcd.substring(0,1));
            int y = Integer.parseInt(fspl.substring(0, 1));
            int z = x * y;
            String risk = (z >= 15) ? risk1 : ( z < 8 ? risk3 : risk2);
            // 风险等级
            cell = change.getCell(Integer.valueOf(1547));
            CellUtils.setListCellValue(cell, new String[] {risk});
            result = new ActionResult(ActionResult.STRING, "计算 [风险等级] 值为 "+risk);
        } catch (Exception e) {
            e.printStackTrace(System.out);
            result = new ActionResult(ActionResult.EXCEPTION, e);
        }
        return result;
    }
}
